<?php

/**
 * BaseMongo.class.php
 * 基础类
 */

class BaseMongo
{
    protected static $_instance;

//     private function __construct(){}

    private function __clone(){}

    public static function get_mongoInstance ()
    {
        if (empty(self::$_instance)) {
            /* 获取配置文件 */
            $config = (require_once INTERFACE_PATH . '/config/mongodb.conf.php');
            
            /* 链接mongo */
            if (! $connection = lib\Monga::connection($config['mongo']['dsn'])) {
                throw new \Exception('mongo connection failed', - 999);
            }
            
            self::$_instance = $connection->database($config['mongo']['database']);
        }
        
        return self::$_instance;
    }

    /**
     * [std_class_object_to_array 将对象转成数组]
     * 
     * @param [stdclass] $stdclassobject [对象]
     * @return [array] [数组]
     */
    protected function std_class_object_to_array ($stdclassobject)
    {
        $_array = is_object($stdclassobject) ? get_object_vars($stdclassobject) : $stdclassobject;
        foreach ($_array as $key => $value) {
            $value = (is_array($value) || is_object($value)) ? std_class_object_to_array($value) : $value;
            $array[$key] = $value;
        }
        return $array;
    }
}